package view.user;


import model.booking.Booking;
import model.user.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
/*
 * Created by JFormDesigner on Tue May 28 15:56:36 HKT 2024
 */



/**
 * @author 11565
 */
public class FillInInformationPanel extends JPanel {
    private Mainframe frame;
    private User user;
    public FillInInformationPanel(User user, Mainframe frame) {
        initComponents();
        this.frame = frame;
        this.user = user;
    }

    private void BackButton1(ActionEvent e) {
        // TODO add your code here
        frame.showTimePanel();
    }

    //确认预约
    private void ConfrimButton1(ActionEvent e) {
        // TODO add your code here
        String name = NametextField.getText();
        String ID = IDtextField.getText();
        if(name == "" || ID == "" || frame.getRoomTypes() == null){
            JOptionPane.showMessageDialog(null, "未选择必要信息，请重新预约!");
            frame.showTimePanel();
            return;
        }
        Booking booking = new Booking(user,name,ID,frame.getStartTime(),frame.getEndTime(),frame.getRoomTypes());
        if(!Booking.isFull(booking.getCheckInTime(),booking.getCheckOutTime(),booking.getRoomTypes())){
            if(Booking.isExist(booking)) {
                //弹出请勿重复预约
                JOptionPane.showMessageDialog(null, "请勿重复预约");
            }
            else{
                Booking.addBooking(booking);
                //弹出预约成功
                JOptionPane.showMessageDialog(null,"预约成功");
            }
        }else{
            //弹出预约失败
            JOptionPane.showMessageDialog(null,"预约失败,房间已订满!");
        }
        frame.showTimePanel();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - Qiqq
        panel1 = new JPanel();
        panel2 = new JPanel();
        label1 = new JLabel();
        NametextField = new JTextField();
        label2 = new JLabel();
        IDtextField = new JTextField();
        ConfrimButton1 = new JButton();
        BackButton1 = new JButton();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing. border.
        EmptyBorder( 0, 0, 0, 0) , "JF\u006frmDes\u0069gner \u0045valua\u0074ion", javax. swing. border. TitledBorder. CENTER, javax. swing
        . border. TitledBorder. BOTTOM, new java .awt .Font ("D\u0069alog" ,java .awt .Font .BOLD ,12 ),
        java. awt. Color. red) , getBorder( )) );  addPropertyChangeListener (new java. beans. PropertyChangeListener( )
        { @Override public void propertyChange (java .beans .PropertyChangeEvent e) {if ("\u0062order" .equals (e .getPropertyName () ))
        throw new RuntimeException( ); }} );

        //======== panel1 ========
        {
            panel1.setLayout(new CardLayout());

            //======== panel2 ========
            {

                //---- label1 ----
                label1.setText("\u59d3\u540d\uff1a");

                //---- label2 ----
                label2.setText("\u8eab\u4efd\u8bc1\u53f7\uff1a");

                //---- ConfrimButton1 ----
                ConfrimButton1.setText("\u786e\u8ba4");
                ConfrimButton1.addActionListener(e -> ConfrimButton1(e));

                //---- BackButton1 ----
                BackButton1.setText("\u8fd4\u56de");
                BackButton1.addActionListener(e -> BackButton1(e));

                GroupLayout panel2Layout = new GroupLayout(panel2);
                panel2.setLayout(panel2Layout);
                panel2Layout.setHorizontalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(panel2Layout.createSequentialGroup()
                            .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addGroup(panel2Layout.createSequentialGroup()
                                    .addContainerGap(195, Short.MAX_VALUE)
                                    .addComponent(BackButton1)
                                    .addGap(31, 31, 31)
                                    .addComponent(ConfrimButton1))
                                .addGroup(panel2Layout.createSequentialGroup()
                                    .addGap(36, 36, 36)
                                    .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                                        .addComponent(label1, GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))
                                    .addGap(18, 18, 18)
                                    .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(NametextField, GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                        .addComponent(IDtextField, GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)))
                            .addContainerGap(83, Short.MAX_VALUE))
                );
                panel2Layout.setVerticalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(panel2Layout.createSequentialGroup()
                            .addGap(34, 34, 34)
                            .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label1)
                                .addComponent(NametextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label2)
                                .addComponent(IDtextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 132, Short.MAX_VALUE)
                            .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(ConfrimButton1)
                                .addComponent(BackButton1))
                            .addGap(61, 61, 61))
                );
            }
            panel1.add(panel2, "card1");
        }

        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - Qiqq
    private JPanel panel1;
    private JPanel panel2;
    private JLabel label1;
    private JTextField NametextField;
    private JLabel label2;
    private JTextField IDtextField;
    private JButton ConfrimButton1;
    private JButton BackButton1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
